package model

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"goods_srv/global"
)

type EsGoods struct {
	Id          int32   `json:"id"`
	CategoryID  int32   `json:"category_id"`
	BrandsID    int32   `json:"brands_id"`
	OnSale      bool    `json:"on_sale"`
	ShipFree    bool    `json:"ship_free"`
	IsNew       bool    `json:"is_new"`
	IsHot       bool    `json:"is_hot"`
	Name        string  `json:"name"`
	ClickNum    int32   `json:"click_num"`
	SoldNum     int32   `json:"sold_num"`
	FavNum      int32   `json:"fav_num"`
	MarketPrice float32 `json:"market_price"`
	ShopPrice   float32 `json:"shop_price"`
	GoodsBrief  string  `json:"goods_brief"`
}

func (EsGoods) GetIndexName() string {
	return "goods"
}

func (EsGoods) GetMapping() string {
	goodsMapping := `
{
		"mappings" : {
			"properties" : {
				"brands_id" : {
					"type" : "integer"
				},
				"category_id" : {
					"type" : "integer"
				},
				"click_num" : {
					"type" : "integer"
				},
				"fav_num" : {
					"type" : "integer"
				},
				"id" : {
					"type" : "integer"
				},
				"is_hot" : {
					"type" : "boolean"
				},
				"is_new" : {
					"type" : "boolean"
				},
				"market_price" : {
					"type" : "float"
				},
				"name" : {
					"type" : "text",
					"analyzer":"ik_max_word"
				},
				"goods_brief" : {
					"type" : "text",
					"analyzer":"ik_max_word"
				},
				"on_sale" : {
					"type" : "boolean"
				},
				"ship_free" : {
					"type" : "boolean"
				},
				"shop_price" : {
					"type" : "float"
				},
				"sold_num" : {
					"type" : "long"
				}
			}
		},
	"settings": {
	       "number_of_shards": 1,
		   "number_of_replicas": 0
  		}
	}`
	return goodsMapping
}

func (EsGoods) MysqlByEsBulk() {
	goods := []Goods{}
	global.DB.Model(&Goods{}).Find(&goods)
	bulk := global.EsClient.Bulk()
	for _, v := range goods {
		doc := elastic.NewBulkIndexRequest().Index(EsGoods{}.GetIndexName()).Id(string(v.ID)).Doc(v)
		bulk = bulk.Add(doc)
	}
	_, err := bulk.Do(context.Background())
	if err != nil {
		fmt.Println("Failed to execute bulk ")
		return
	}
	fmt.Println("successfully execute bulk")
}
